#include "Battle.h"
#include <vector>
#include <time.h>

Battle::Battle()
{
	
}
Battle::~Battle()
{

}
Battle::Battle(Dinosaur attacker, Dinosaur defender)
{
	DinoList.push_back(attacker);
	DinoList.push_back(defender);
}
Battle::Battle(Dinosaur attacker, Dinosaur defender1, Dinosaur defender2)
{
	DinoList.push_back(attacker);
	DinoList.push_back(defender1);
	DinoList.push_back(defender2);
}
int Battle::getStart()
{
	return start;
}
void Battle::setStart(int Start)
{
	start=Start;
}
vector<Dinosaur>Battle::SimBattle()
{
	srand(time(NULL));
	int damage;
	if(DinoList.size()==2)
	{
		while(1)
		{
			damage=0;
			for(int x=10; x>0; x--)
			{
				if(DinoList[1].getSpeed()==x)
				{
					damage=((rand()%3+1)*DinoList[1].getAttack())-DinoList[0].getDefense();
					if(damage>0)
						DinoList[0].setCurHealth(DinoList[0].getCurHealth()-damage);
				}
				if(DinoList[0].getSpeed()==x)
				{
					damage=((rand()%3+1)*DinoList[0].getAttack())-DinoList[1].getDefense();
					if(damage>0)
						DinoList[1].setCurHealth(DinoList[1].getCurHealth()-damage);
				}
			}
			if(DinoList[1].getCurHealth()<=DinoList[1].getMaxHealth()*.25)
			{
				if(1==rand()%5&&!DinoList[1].getCarn())
					DinoList[1].setFlee(true);
			}
			if(DinoList[1].getCurHealth()<=0)
				DinoList[0].eat();
			if(DinoList[0].getCurHealth()<=0||DinoList[1].getCurHealth()<=0||DinoList[1].getFlee())
				break;
		}

	}
	if(DinoList.size()==3)
	{
		while(1)
		{
			damage=0;
			for(int x=10; x>0; x--)
			{
				if(DinoList[1].getSpeed()==x)
				{
					damage=((rand()%3+1)*DinoList[1].getAttack())-DinoList[0].getDefense();
					if(damage>0)
						DinoList[0].setCurHealth(DinoList[0].getCurHealth()-damage);
				}
				if(DinoList[2].getSpeed()==x&&DinoList[2].getCurHealth()>0)
				{
					damage=((rand()%3+1)*DinoList[2].getAttack())-DinoList[0].getDefense();
					if(damage>0)
						DinoList[0].setCurHealth(DinoList[0].getCurHealth()-damage);
				}
				if(DinoList[0].getSpeed()==x)
				{
					if(DinoList[2].getCurHealth()>0)
					{
						damage=((rand()%3+1)*DinoList[0].getAttack())-DinoList[2].getDefense();
						if(damage>0)
							DinoList[2].setCurHealth(DinoList[2].getCurHealth()-damage);
					}
					else
					{
						damage=((rand()%3+1)*DinoList[0].getAttack())-DinoList[1].getDefense();
						if(damage>0)
							DinoList[1].setCurHealth(DinoList[1].getCurHealth()-damage);
					}
				}
			}
			if(DinoList[1].getCurHealth()<=DinoList[1].getMaxHealth()*.25)
			{
				if(1==rand()%5&&!DinoList[1].getCarn())
					DinoList[1].setFlee(true);
			}
			if(DinoList[0].getCurHealth()<=0||DinoList[1].getCurHealth()<=0||DinoList[1].getFlee())
			{
				if(DinoList[1].getCurHealth()<=0)
					DinoList[0].eat();
				if(DinoList[2].getCurHealth()<=0)
					DinoList[0].eat();
				break;
			}
		}

	}
	return DinoList;
}